﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace antyspam_filter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        private antyspam_filter.Filter f;
        SqlConnection conn;
        DataClasses1DataContext db;

        public MainWindow()
        {
            InitializeComponent();
            conn = new SqlConnection(antyspam_filter.Properties.Settings.Default.sbw_dbConnectionString1);
            refreshBlackList();
            refreshWhiteList();
            refreshBlockedWordsList();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            f = new Filter();
            if (f.filterEmail(EmailContent.Text))
            {
                FilterStatus.Fill = new SolidColorBrush(Color.FromRgb(0, 255, 0));
            }
            else
            {
                FilterStatus.Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            }
        }

        private void ButtonBlack_Click(object sender, RoutedEventArgs e)
        {
            
            if (blackComboBox.SelectedItem != null)
            {
                db = new DataClasses1DataContext();

                List row = new List();
                row.category = (short)blackComboBox.SelectedIndex;
                row.type = true;
                row.data = blackTextBox.Text;

                db.Lists.InsertOnSubmit(row);
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }
                refreshBlackList();
            }
        }

        private void WhiteButton_Click(object sender, RoutedEventArgs e)
        {
            if (whiteComboBox.SelectedItem != null)
            {
                db = new DataClasses1DataContext();

                List row = new List();
                row.category = (short)whiteComboBox.SelectedIndex;
                row.type = false;
                row.data = whiteTextBox.Text;

                db.Lists.InsertOnSubmit(row);
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }
                refreshWhiteList();
            }
        }

        private void refreshBlackList()
        {
            blackListView.Items.Clear();

            db = new DataClasses1DataContext();
            
            var rows = from l in db.Lists
                       where l.type == true
                       select l;

            foreach (List l in rows)
            {
                blackListView.Items.Add(l.data);
            }
        }

        private void refreshWhiteList()
        {
            whiteListView.Items.Clear();

            db = new DataClasses1DataContext();

            var rows = from l in db.Lists
                       where l.type == false
                       select l;

            foreach (List l in rows)
            {
                whiteListView.Items.Add(l.data);
            }
        }

        private void buttonDeleteFromBlackList_Click(object sender, RoutedEventArgs e)
        {
            if (blackListView.SelectedIndex != -1)
            {
                db = new DataClasses1DataContext();

                var row = from l in db.Lists
                           where l.data == blackListView.SelectedValue
                           select l;

                db.Lists.DeleteAllOnSubmit(row);

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }

                refreshBlackList();
            }
        }

        private void buttonDeleteFromWhiteList_Click(object sender, RoutedEventArgs e)
        {
            if (whiteListView.SelectedIndex != -1)
            {
                db = new DataClasses1DataContext();

                var row = from l in db.Lists
                          where l.data == whiteListView.SelectedValue
                          select l;

                db.Lists.DeleteAllOnSubmit(row);

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }

                refreshWhiteList();
            }
        }

        private void addWordButton_Click(object sender, RoutedEventArgs e)
        {

            if (wordValueTextBox.Text.Length > 0)
            {
                db = new DataClasses1DataContext();

                BlockedWord row = new BlockedWord();
                row.wordValue = wordValueTextBox.Text;

                db.BlockedWords.InsertOnSubmit(row);
                try
                {
                    db.SubmitChanges();
                
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }
                refreshBlockedWordsList();
            }
        }

        private void refreshBlockedWordsList()
        {
            BlockedWordListView.Items.Clear();

            db = new DataClasses1DataContext();

            var rows = from l in db.BlockedWords
                       select l;

            foreach (BlockedWord l in rows)
            {
                BlockedWordListView.Items.Add(l);
            }
        }

        private void deleteBlockedWordButton_Click(object sender, RoutedEventArgs e)
        {
            if (BlockedWordListView.SelectedIndex != -1)
            {
                db = new DataClasses1DataContext();

                ListViewItem item =
                    (ListViewItem)(BlockedWordListView.ItemContainerGenerator.ContainerFromItem(BlockedWordListView.SelectedItem));

                ContentPresenter presenter = FindVisualChild<ContentPresenter>(item);
                DataTemplate temp = presenter.ContentTemplate;

                TextBlock tb = (TextBlock)temp.FindName("blockedWordValueTextBlock", presenter);

                var row = from l in db.BlockedWords
                          where l.wordValue == tb.Text
                          select l;

                db.BlockedWords.DeleteAllOnSubmit(row);

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    db.SubmitChanges();
                }

                refreshBlockedWordsList();
            }
        }

        private childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}
